//
//  WBMainController.swift
//  WeiBo(swift版)
//
//  Created by 胡卓 on 2017/4/2.
//  Copyright © 2017年 胡卓. All rights reserved.
//

import UIKit

class WBMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
    
        setupUI ()
    }

}



// MARK: - 点击事件
extension WBMainController {
    
    @objc fileprivate func compose(button: UIButton) {
        
        print("发布微博。。。")
    }
}


// MARK: - 设置UI
extension WBMainController {
    
    fileprivate func setupUI () {
        
        self.view.backgroundColor = UIColor.white
        //添加子控制器
        addChildVC()
        
        //添加底部按钮
        setBottomButton()
        
        //设置tabbar的阴影线条
        setupShadowImage()
    }
    
    //设置tabbar的阴影线条，以下两个属性都要设置
    func setupShadowImage() {
        
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.shadowImage = UIImage.pureImage(color:UIColor(white: 0.9, alpha: 0.9))
    }
    
    
    fileprivate func setBottomButton() {
        
        let button = UIButton(title: nil,image: "tabbar_compose_icon_add", bgImage: "tabbar_compose_button", target: self, action: #selector(compose(button:)))
        
        //设置按钮的frame
        //insetBy 在前面的frame为中心点 向内或者向外扩展
        let width = tabBar.frame.size.width / 5
        
        button.frame = tabBar.bounds.insetBy(dx: width * 2 - 2, dy: 6)
        
        //将按钮添加到tabbar上
        tabBar.addSubview(button)
    }
    
    
    
    //添加子控制器
    fileprivate func addChildVC() {
        
        if let url = Bundle.main.url(forResource: "main.json", withExtension: nil),
            //使用url，获取data
            let jsonData = try? Data(contentsOf: url),
            //使用jsonData 反序列化
            let obj = try? JSONSerialization.jsonObject(with: jsonData, options: []),
            //进行类型转换
            let dictArr = obj as? [[String :Any]] {
            
            //创建空数组
            var controllers: [UINavigationController] = []
            
            //        print(dictArr)
            
            //遍历参数字典
            for dict in dictArr {
                //创建子控制器
                let nav = makeChildVCs(dict: dict)
                controllers.append(nav!)
            }
            
            //设置所有的子控制器
            self.viewControllers = controllers
            
        }
    }
    
    //创建单个子控制器
    fileprivate func makeChildVCs(dict: [String: Any]) -> UINavigationController? {
        //通过字符串创建类，并用类来创建控制器
        if  let className = dict["clsName"] {
            //类名在swift中，必须加上项目名的前缀
            let name = "weiBo" + "." + "\(className as! String)"
            
            //用字符串来创建类  必须转成 Type类型
            if let cls = NSClassFromString(name) as? UIViewController.Type {
                
                //用类来创建子控制器对象
                let vc = cls.init()
                
                //给子控制器设置title
                let title = dict["title"] as? String
                vc.title = title
                vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: .normal)
                vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:
                    UIColor.orange], for: .selected)
                
                //给子控制器设置tabbarItem图片
                if let imgName = dict["imageName"] {
                    
                    let image = UIImage(named: "tabbar_\(imgName)")?.withRenderingMode(.alwaysOriginal)
                    let selectedImage = UIImage(named: "tabbar_\(imgName)_selected")?.withRenderingMode(.alwaysOriginal)
                    
                    vc.tabBarItem.image = image
                    vc.tabBarItem.selectedImage = selectedImage
                }
                //使用子控制器来创建UINavigationController
                let nav = UINavigationController(rootViewController: vc)
                return nav
            }
        }
        return nil
    }
}

